package leetcode;

public class LicenseKeyFormatting {

	public static void main(String[] args) {
		LicenseKeyFormatting object = new LicenseKeyFormatting();
//		String string = "2-4A0r7-";
//		int k = 3;
//		System.out.println(object.licenseKeyFormatting(string, k));
		String[] strings = {"aaa", "aaa", "aa"};
		System.out.println(object.findLUSlength(strings));
	}
	
    public String licenseKeyFormatting(String s, int k) {
        if(s == null || k == 0){
        	return s;
        }
        int length = s.length();
        if(length == 0){
        	return s;
        }
        StringBuilder sBuilder = new StringBuilder();
        for(int i = 0; i < length; i++){
        	char c = s.charAt(i);
        	if(c == '-'){
        		continue;
        	}
        	if(c >= 'a' && c <= 'z'){
        		c = (char)(c - 32);
        	}
        	sBuilder.append(c);
        }
        length = sBuilder.length();
        System.out.println("length: " + length);
        int dividor = length / k;
        int mod = length - dividor * k;
        if(mod == 0){
        	dividor = dividor - 1;
        	mod = k;
        }
        for(int i = 0; i < dividor; i++){
        	sBuilder.insert(i * k + mod + i, '-');
        }
        return sBuilder.toString();
    }
    
    //不能通过测试： "aaa", "aaa", "aa"
    public int findLUSlength(String[] strs) {
        if(strs == null || strs.length <= 0){
            return 0;
        }
        boolean allIsSame = true;
        int maxLength = 0;
        int secondLength = 0;
        for(int i = 0; i < strs.length; i++){
            int length = strs[i].length();
            if(!strs[i].equals(strs[0])){
                allIsSame = false;
            }
            if(length > maxLength){
                maxLength = length;
                secondLength = maxLength;
            }else if(length > secondLength){
                secondLength = length;
            }
        }
        System.out.println(allIsSame);
        if(maxLength != secondLength){
            return maxLength;
        }else if(allIsSame){
            return -1;
        }else{
            return maxLength;
        }
        
    }
}
